package com.mine.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mine.R;

import java.util.List;

public class ProfileOptionAdapter extends RecyclerView.Adapter<ProfileOptionAdapter.OptionViewHolder> {

    private List<ProfileOption> optionList;
    private OnOptionClickListener listener;

    public ProfileOptionAdapter(List<ProfileOption> optionList) {
        this.optionList = optionList;
    }

    public void setOnOptionClickListener(OnOptionClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public OptionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_profile_option, parent, false);
        return new OptionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OptionViewHolder holder, int position) {
        ProfileOption option = optionList.get(position);
        holder.optionIcon.setImageResource(option.getIconResId());
        holder.optionName.setText(option.getName());
    }

    @Override
    public int getItemCount() {
        return optionList.size();
    }

    public class OptionViewHolder extends RecyclerView.ViewHolder {
        ImageView optionIcon;
        TextView optionName;

        public OptionViewHolder(@NonNull View itemView) {
            super(itemView);
            optionIcon = itemView.findViewById(R.id.option_icon);
            optionName = itemView.findViewById(R.id.option_name);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onOptionClick(position);
                        }
                    }
                }
            });
        }
    }

    public interface OnOptionClickListener {
        void onOptionClick(int position);
    }

    public static class ProfileOption {
        private int iconResId;
        private String name;

        public ProfileOption(int iconResId, String name) {
            this.iconResId = iconResId;
            this.name = name;
        }

        public int getIconResId() {
            return iconResId;
        }

        public String getName() {
            return name;
        }
    }
}